package com.silence.gmall.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author silence
 */
@Configuration
@ConditionalOnProperty(prefix = "spring.data.redis", name = {"host", "port"})
public class RedissonConfig {

    /**
     * Redis服务器地址
     */
    @Value("#{'redis://${spring.data.redis.host}:${spring.data.redis.port}'}")
    private String redisAddress;

    /**
     * Redis服务器密码
     */
    @Value("${spring.data.redis.password}")
    private String password;

    /**
     * 配置Redisson客户端Bean
     *
     * @return 返回一个配置好的RedissonClient实例
     * destroyMethod = "shutdown" 指定Bean销毁时调用shutdown方法释放资源
     */
    @Bean(destroyMethod = "shutdown")
    public RedissonClient redissonClient() {
        // 创建Redisson配置对象
        Config config = new Config();
        // 配置使用单服务器模式
        config.useSingleServer()
                .setAddress(redisAddress)
                .setPassword(password);
        return Redisson.create(config);
    }
}
